#pragma once
#include "../widgets.h"
#include <QObject>
#include <map>

enum ColWidthType
{
	kFixed,
	kAuto,
	kManual
};
struct S_WIDGETS_EXPORT ColumnInfo
{
	QString	name;
	int		index = 0;//递增，不能跳
	int      curIndex = 0;
	int		group = 0;
	float   ratio = -1;
	float    minWidth = -1;
	float    dftWidth = -1;
	float    curWidth = -1;
	bool	hide = false;
	Qt::Alignment align = Qt::AlignVCenter | Qt::AlignLeft;
	ColWidthType  widthType = kManual;

	ColumnInfo() = default;
	ColumnInfo(const ColumnInfo&) = default;
};

#define BEGIN_COLUMN int colIndex = 0;

#define ADD_COLUMN(colSet, name1, group1, dftWidth1, minWidth1, ratio1, hide1, align1, widthType1) \
{\
	ColumnInfo ci;\
	ci.index = colIndex; ci.name = name1; ci.group = group1; ci.ratio = ratio1; ci.minWidth = minWidth1; ci.dftWidth = dftWidth1;\
	ci.hide = hide1; ci.align = align1; ci.widthType = widthType1; ci.curWidth = dftWidth1; ci.curIndex = colIndex;\
	colSet->addColumnInfo(ci);\
	++colIndex;\
}

class SVirtualTable;
class S_WIDGETS_EXPORT STableColumnSet : public QObject
{
	Q_OBJECT
public:
	STableColumnSet(SVirtualTable*, const QString&, QObject*);
	void addColumnInfo(const ColumnInfo&);
	void setTableWidthFixed(bool fixed);
	void apply(bool loadCfg = true);
	void setSaveCfg(bool v);
	void restoreDft();
	void resizeColumn(bool useDft = false);
	void applyChange(std::map<int, bool>);
	bool isColVisible(int index);
	ColumnInfo colInfo(int index);

	std::map<int, std::vector<ColumnInfo> > groups();
protected:
	bool eventFilter(QObject* watched, QEvent* event) override;

	void saveCfg();
	void loadCfg();
protected slots:
	void onHeaderSectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex);
	void onHeaderSectionResized(int logicalIndex, int oldSize, int newSize);

private:
	QString	m_idStr;
	std::map<int, ColumnInfo>	m_dftColumnInfos;//first is logic index
	std::map<int, ColumnInfo>	m_curColumnInfos;//first is visual index
	SVirtualTable* m_table = nullptr;
	bool	m_tableWidthFixed = false; //根据Table的宽带自动调整列宽
	bool	m_saveCfg = false;
	bool	m_doResize = false;
	bool    m_doApply = false;
	bool    m_dragHeader = false;
};